package messagepasser;

/**
 * 
 * @author Rishit Shroff(rshroff)
 * @author Huimin Yang(huiminy)
 * @description Contains the description of the Message class 
 */

import java.io.Serializable;

/**
 * 
 * a message is constituted by header and payload
 * 
 * @see Header
 *
 */
public class Message implements Serializable, Comparable{
	protected Header header;
	protected Object payload;
	boolean isloggable = true;
	protected boolean multicastMsg; // change here
	protected String msgType;
	protected String[] multiDestList; // I need to parse the destination too many times so create this field...
	
	//debug
	protected String physicalSender;
	
	
	public boolean isIsloggable() {
		return isloggable;
	}

	public void setIsloggable(boolean isloggable) {
		this.isloggable = isloggable;
	}

	public boolean isMulticastMsg() {
		return multicastMsg;
	}

	public void setMulticastMsg(boolean multicastMsg) {
		this.multicastMsg = multicastMsg;
	}

	public String getMsgType() {
		return msgType;
	}

	public void setMsgType(String msgType) {
		this.msgType = msgType;
	}

	public Message(String dest, String src, String msg_class, String id, Object data) {
		header = new Header(dest, src, msg_class, id);
		payload = data;
		multiDestList = this.initializeMultiDest();
	}
	
	public Message(String dest, String src, String msg_class, String id, Object data, boolean log, boolean multi) {
		header = new Header(dest, src, msg_class, id);
		payload = data;
		isloggable = log;
		multicastMsg = multi; // change here
		multiDestList = this.initializeMultiDest();
	}
	
	
	
	public Header getHeader() {
		return header;
	}
	
	public Object getPayload() {
		return payload;
	}
	
	public Message(Header header, Object data) {
		this.header = header;
		this.payload = data;
	}

	public void setPayload(Object payload) {
		this.payload = payload;
	}

	public String[] initializeMultiDest() {
		String[] destination = header.getDest().split(",");
		for(int i = 0; i < destination.length; i++) {
			String dest = destination[i];
			destination[i] = dest.trim();
		}
		return destination;
	}
	
	public String[] getMultiDestination() {
		return multiDestList;
	}
	

	@Override
	public int compareTo(Object arg0) {return 0;}
	
	public String getPhySender() {
		return physicalSender;
	}
	
	public void setPhySender(String psend) {
		physicalSender = psend;
	}
	
	public boolean isLockMsg() {
		if (msgType != null) {
			return msgType.equals("lock");
		}
		return false;
	}
	
}

